package com.sikao.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.MyTendersRecordBean;
import com.sikao.app.myinterface.CallbackOne;
import com.sikao.app.utils.TimeUtil;

import java.util.List;

/**
 * Created by y50 on 2016/5/6.
 */
public class MyTendersAdapter extends BaseAdapter {

	private Context context;
	private List<MyTendersRecordBean.ResBean.ListBean> beans;
	private CallbackOne callbackOne;

	public MyTendersAdapter(Context context, List<MyTendersRecordBean.ResBean.ListBean> beans, CallbackOne callbackOne) {
		this.context = context;
		this.beans = beans;
		this.callbackOne = callbackOne;
	}

	@Override
	public int getCount() {
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder viewHolder;
		if (convertView == null) {
			view = View.inflate(context, R.layout.my_tenders_list_item, null);
			viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.btn2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				callbackOne.notice("投标记录", position);
			}
		});
		viewHolder.tvname.setText(beans.get(position).getTitle());
		viewHolder.tvpriceunit.setText("元/" + beans.get(position).getUnit());
		viewHolder.tvprice.setText(beans.get(position).getPrice());
		viewHolder.tvamount.setText("数量" + beans.get(position).getOrigin_number() + beans.get(position).getUnit());
		viewHolder.tvtime.setText(TimeUtil.timeStampToTime(beans.get(position).getAdd_time(), TimeUtil.fotmatString2));
		viewHolder.btn2.setVisibility(View.VISIBLE);
		//商品状态 - 1 准备资料0待审核 1 通过 2 审核失败3下架 4 售空
		viewHolder.tvstatus.setTextColor(context.getResources().getColor(R.color.text_red));
		if (beans.get(position).getStatus().equals("-1")) {
			viewHolder.btn1.setText("下架");
			viewHolder.tvstatus.setText("准备资料");
			//	viewHolder.btn2.setVisibility(View.GONE);
		} else if (beans.get(position).getStatus().equals("0")) {
			viewHolder.btn1.setText("下架");
			viewHolder.tvstatus.setText("待审核");
			//viewHolder.btn2.setVisibility(View.GONE);
		} else if (beans.get(position).getStatus().equals("1")) {
			viewHolder.btn1.setText("下架");
			viewHolder.tvstatus.setText("审核通过");
		} else if (beans.get(position).getStatus().equals("2")) {
			viewHolder.btn1.setText("下架");
			viewHolder.tvstatus.setText("审核失败");
			//viewHolder.btn2.setVisibility(View.GONE);
		} else if (beans.get(position).getStatus().equals("3")) {
			if (beans.get(position).getIs_backed() != 1 && beans.get(position).getPublish_type().equals("2")) {
				viewHolder.btn1.setText("退还保证金");
			} else {
				viewHolder.btn1.setText("删除");
			}
			viewHolder.tvstatus.setText("已下架");
			viewHolder.tvstatus.setTextColor(context.getResources().getColor(R.color.text_lighter_gray));
		} else if (beans.get(position).getStatus().equals("4")) {
			viewHolder.tvstatus.setText("售空");
			viewHolder.btn1.setText("下架");
		}
		viewHolder.btn1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (beans.get(position).getStatus().equals("3")) {
					if (beans.get(position).getIs_backed() != 1 && beans.get(position).getPublish_type().equals("2")) {
						callbackOne.notice("returnDeposit", position);
					} else {
						callbackOne.notice("delete", position);
					}
				} else {
					callbackOne.notice("offShelve", position);
				}
			}
		});
		return view;
	}

	public class ViewHolder {
		public final TextView tvname;
		public final TextView tvstatus;
		public final TextView tvpriceunit;
		public final TextView tvprice;
		public final TextView tvamount;
		public final TextView tvtime;
		public final Button btn2;
		public final Button btn1;
		public final View root;

		public ViewHolder(View root) {
			tvname = (TextView) root.findViewById(R.id.tv_name);
			tvstatus = (TextView) root.findViewById(R.id.tv_status);
			tvpriceunit = (TextView) root.findViewById(R.id.tv_price_unit);
			tvprice = (TextView) root.findViewById(R.id.tv_price);
			tvamount = (TextView) root.findViewById(R.id.tv_amount);
			tvtime = (TextView) root.findViewById(R.id.tv_time);
			btn2 = (Button) root.findViewById(R.id.btn2);
			btn1 = (Button) root.findViewById(R.id.btn1);
			this.root = root;
		}
	}
}
